package com.tools.scanner.ui.qrcode.scanner

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.mlkit.vision.barcode.common.Barcode
import com.tools.scanner.R
import com.tools.scanner.databinding.FragmentContactScannerBinding
import com.tools.scanner.utils.saveContact

class ContactFragment : BaseScannerFragment() {

    private lateinit var mBinding: FragmentContactScannerBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentContactScannerBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val contactInfo = getData(Barcode.ContactInfo::class.java) ?: return

        mBinding.tvName.text = contactInfo.name?.formattedName

        val phones = contactInfo.phones
        if (phones.isNotEmpty()) {
            val stringBuilder = StringBuilder()
            phones.forEach {
                val type = when (it.type) {
                    Barcode.Phone.TYPE_MOBILE -> {
                        resources.getString(R.string.input_type_call_phone)
                    }

                    Barcode.Phone.TYPE_WORK -> {
                        resources.getString(R.string.input_type_work_phone)
                    }

                    Barcode.Phone.TYPE_HOME -> {
                        resources.getString(R.string.input_type_phone)
                    }

                    Barcode.Phone.TYPE_FAX -> {
                        resources.getString(R.string.input_type_fax)
                    }

                    else -> {
                        resources.getString(R.string.input_type_unknown_phone)
                    }
                }
                stringBuilder.append(type).append(":")
                stringBuilder.append(it.number).append("\n")
            }
            mBinding.tvPhone.text = stringBuilder.toString().trimEnd('\n')
        }

        val emails = contactInfo.emails
        if (emails.isNotEmpty()) {
            val stringBuilder = StringBuilder()
            emails.forEach {
                val type = when (it.type) {
                    Barcode.Email.TYPE_HOME -> {
                        resources.getString(R.string.input_type_email)
                    }

                    Barcode.Email.TYPE_WORK -> {
                        resources.getString(R.string.input_type_work_email)
                    }

                    else -> {
                        resources.getString(R.string.input_type_unknown_phone)
                    }
                }
                stringBuilder.append(type).append(":")
                stringBuilder.append(it.address).append("\n")
            }
            mBinding.tvEmail.text = stringBuilder.toString().trimEnd('\n')
        }

        mBinding.tvOrganization.text = contactInfo.organization
        mBinding.tvTitle.text = contactInfo.title

        val addresses = contactInfo.addresses
        if (addresses.isNotEmpty()) {
            val stringBuilder = StringBuilder()
            addresses.forEach {
                val addressLines = it.addressLines
                val address = addressLines.joinToString("\n")
                stringBuilder.append(address).append("\n")
            }
            mBinding.tvAddress.text = stringBuilder.toString().trimEnd('\n')
        }

        mBinding.tvSave.setOnClickListener {
            contactInfo.saveContact()
        }
    }

}